# -*- coding: utf-8 -*-
import os,json,gzip, toml, time
from flask import current_app, flash
from datetime import datetime as cdatetime  # 有时候会返回datatime类型
from datetime import date, time
from flask_sqlalchemy import Model
from sqlalchemy.orm.query import Query
from sqlalchemy import DateTime, Numeric, Date, Time  # 有时又是DateTime
from sqlalchemy.sql import func
from adminApp.extensions import db, redis_store


def get_sys_config_obj():
    # 获取应用的配置，并转换
    val = redis_store.get(current_app.config.get('APPCONFIG_TOML_KEY'))
    data = val.decode() if val else None
    if data is None:
        flash('配置文件为空', 'warning')
    else:
        try:
            return toml.loads(data)
        except:
            flash('配置文件加载失败', 'error')
            return {}


def queryToDict(models):
    if (isinstance(models, list)):
        if (isinstance(models[0], Model)):
            lst = []
            for model in models:
                gen = model_to_dict(model)
                dit = dict((g[0], g[1]) for g in gen)
                lst.append(dit)
            return lst
        else:
            res = result_to_dict(models)
            return res
    else:
        if (isinstance(models, Model)):
            gen = model_to_dict(models)
            dit = dict((g[0], g[1]) for g in gen)
            return dit
        else:
            res = dict(zip(models.keys(), models))
            find_datetime(res)
            return res


# 当结果为result对象列表时，result有key()方法
def result_to_dict(results):
    res = [dict(zip(r.keys(), r)) for r in results]
    # 这里r为一个字典，对象传递直接改变字典属性
    for r in res:
        find_datetime(r)
    return res


def model_to_dict(model):  # 这段来自于参考资源
    for col in model.__table__.columns:
        if isinstance(col.type, DateTime):
            value = convert_datetime(getattr(model, col.name))
        elif isinstance(col.type, Numeric):
            value = float(getattr(model, col.name))
        else:
            value = getattr(model, col.name)
        yield (col.name, value)


def find_datetime(value):
    for v in value:
        if (isinstance(value[v], cdatetime)):
            value[v] = convert_datetime(value[v])  # 这里原理类似，修改的字典对象，不用返回即可修改


def convert_datetime(value):
    if value:
        if (isinstance(value, (cdatetime, DateTime))):
            return value.strftime("%Y-%m-%d %H:%M:%S")
        elif (isinstance(value, (date, Date))):
            return value.strftime("%Y-%m-%d")
        elif (isinstance(value, (Time, time))):
            return value.strftime("%H:%M:%S")
    else:
        return ""


def build_recevie_api_path(domain, http_type=None):
    import re
    if not http_type:
        http_type = 'http'
    if re.match('\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}', domain):
        return '{}://{}'.format(http_type, domain)
    elif domain.startswith('http'):
        return domain
    elif domain.startswith('www.'):
        return '{}://{}'.format(http_type, domain)
    else:
        return '{}://www.{}'.format(http_type, domain)
